package cn.ml.sboot11.service;

import cn.ml.sboot11.entity.Student;
import cn.ml.sboot11.entity.StudentExample;
import cn.ml.sboot11.mapper.StudentMapper;
import cn.ml.sboot11.pojo.vo.CountDown;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resources;
import java.text.ParseException;
import java.util.List;

@Service
@Transactional
public class StudentServiceImpl implements StudentService{
    @Autowired
    private StudentMapper studentMapper;
    @Override
    public Student selectOne(int id) {
        return studentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Student> list2(StudentExample example) {
        return studentMapper.selectByExample(example);
    }

    @Override
    public void delete(int id) {
        studentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(StudentExample example) {
        studentMapper.deleteByExample(example);
    }

    @Override
    public void save(Student student) {
        if (student.getId() == null | "".equals(student.getId())){
            studentMapper.insert(student);
        } else {
            if (count(student.getId()) == 0){ //有值说明数据库中有次id不能插入
                studentMapper.insertSelective(student);
            }else {
                System.out.println("有值说明数据库中有次id不能插入");
            }
        }
    }

    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKeySelective(student);
    }

    @Override
    public long count(int id) {
        System.out.println("ID为:"+id);
        StudentExample example = new StudentExample();
        StudentExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        long l = studentMapper.countByExample(example);
        return l;
    }

    @Override
    @Async("doExecutor")
    public void countDown() throws ParseException, InterruptedException {
        CountDown countDown = new CountDown();
        countDown.scheduleJob();
    }
}
